package controller;
import java.util.ArrayList;
import java.util.HashMap;

import project_package.*;
public class SalesController {

	private SalesRecords salesRecordsCtrl; 
	private static SalesController instance = null;
	
	public static SalesController getInstance()
    {
    	if(instance == null)
    	{
    		instance = new SalesController();
    	}
    	return instance;
    }
	
	public SalesController()
	{
		salesRecordsCtrl = SalesRecords.getInstance();
	}
	
	public void printSaleInfo(Sales sale) 
	{
		salesRecordsCtrl.printSaleInfo(sale);
	}
	
	// print all sales records
	public void printSalesRecords()
	{
		salesRecordsCtrl.printSalesRecords();
	}
	
	// create new sale
	public int createSaleRecord(Customer customer, Employee employee)
	{
		return salesRecordsCtrl.createSaleRecord(customer, employee);
	}
	
	// search for sale
	public Sales searchSaleRecord(int id)
	{
		return salesRecordsCtrl.searchSaleRecord(id);
	}
	
	// delete a sale
	public void deleteSaleRecord(Sales sale)
	{
		salesRecordsCtrl.deleteSaleRecord(sale);
	}
	
	// add a product to the cart IF sale is not confirmed 
	public boolean addProductToCart(Sales sale, Product product, int quantity)
	{
		boolean success = false;
		if (sale.getIsConfirmed())
		{ 
			System.out.println("Sale #"+sale.getID()+"is already confirmed and cannot be changed");
			return false;
		}
		else {
			success = salesRecordsCtrl.addProductToCart(sale, product, quantity);
		}
		return success;
	}
	
	// remove a product from the cart IF sale is not confirmed
	public boolean removeProductFromCart(Sales sale, Product product)
	{
		if (sale.getIsConfirmed())
		{ 
			return false;
		}
		else {
			return salesRecordsCtrl.deleteProductFromCart(sale, product);	
		}
	}
	
	// update a product in the cart IF sale is not confirmed
	public boolean updateProductFromCart(Sales sale, Product product, int quantity)
	{
		if (sale.getIsConfirmed())
		{ 
			return false;
		}
		else {
			return salesRecordsCtrl.updateProductFromCart(sale, product, quantity);
		}
	}
	
	// remove all products from cart IF sale is not confirmed
	public void removeAllProductsFromCart(Sales sale)
	{
		if (sale.getIsConfirmed())
		{ 
			System.out.println("Sale #"+sale.getID()+"is already confirmed and cannot be changed");
		}
		else {
			salesRecordsCtrl.emptyProductCart(sale);
		}
	}

	// confirms sell - locks it so it cannot be changed further more. NOT paid, will be in future
	public boolean confirmSell(Sales sale, boolean isCash)
	{
		if (sale.getIsConfirmed())
		{ 
			return false;
			//System.out.println("Sale #"+sale.getID()+"is already confirmed");
		}
		else {
			salesRecordsCtrl.confirmSale(sale, isCash);
			sale.getEmployee().updateSales(sale.getTotal());
			return true;
		}
	}
	
	public boolean finishSell(Sales sale)
	{
		if (!sale.getIsConfirmed()) {
			return false;//System.out.println("Sale #"+sale.getID()+" is not confirmed yet");
		}
		else {
			sale.setIsFinished(true);
			return true;
		}
		
	}
	
	public HashMap<Integer, Sales> getAllSales()
	{
		return salesRecordsCtrl.getAllSales();
	}
	
	public int getTotalItems() 
	{
		return salesRecordsCtrl.getTotalSalesNumber();
	}
	
	public double getCurrentTotal()
	{
		double currentTotal = salesRecordsCtrl.getTotalSalesPrice();
		return currentTotal;
	}
	
	public double getCustomerDiscount(Sales sale)
	{
		double customerDiscount = sale.getCustomer().getDiscount();
		return customerDiscount;
	}

	public ArrayList<SalesLineItem> getProductList(Sales sale) {
		return salesRecordsCtrl.getProductList(sale);
	}
}